﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Controller;
using Pixysoft.Weblications.Sango.Battle;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Dao;

namespace Pixysoft.Weblications.Sango.Thread
{
    class RvrSectionThread : BaseSangoThread
    {
        public override void Run(object state, bool timeOut)
        {
            if (IsDisposed)
                return;

            if (!IsRunning)
                return;

            LoggerHelper.Debug("begin rve stage thread.");

            foreach (RvrSectionTeam team in RvrSectionController.Instance.Dispatch())
            {
                IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

                command.Begin(delegate()
                {
                    bool fightResult = RvrFight(command, team);

                    foreach (string usercode in team.Profiles)
                    {
                        Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(usercode);

                        // TODO

                        long famevalue = SectionFormular.GetSectionRvrFamevalue(0, 0);

                        Entities.Usr_Sectionbattle rvrBattle = UsrSectionBattleDao.CreateSectionBattle(
                            profile, team.Section.Sectionserialcode, team.TargetSection.Sectionserialcode, famevalue, fightResult);

                        command.Insert(rvrBattle);
                    }

                    command.Commit();
                });
            }
        }

        private bool RvrFight(IEntityNoebeTransaction command, RvrSectionTeam team)
        {
            return true;
        }

        public static long interval
        {
            get
            {
                return 1000 * 10;
            }
        }
    }
}
